const int dx[8] = {1, -1, 0, 0, 1, -1, -1, 1};
const int dy[8] = {0, 0, 1, -1, 1, 1, -1, -1};

int shortestPathBinaryMatrix(int** grid, int gridSize, int* gridColSize){
    if (grid[0][0]!=0 || grid[gridSize-1][gridColSize[0]-1]!=0) {
        return -1;
    }
    int queue[10000][2];
    memset(queue,0,sizeof(queue));

    int left = 0;
    int right = 0;
    grid[0][0]=1;
    queue[right][0]=0;
    queue[right++][1]=0;
    int x,y;
    int step = 1;
    while(left < right) {
        int n = right-left;
        for(int i=0;i<n;i++) {
            x=queue[left][0];
            y=queue[left++][1];
            if (x == gridSize-1 && y == gridColSize[0]-1) {
                return step;
            }
            for(int j=0; j<8;j++) {
                int mx = x + dx[j];
                int my = y + dy[j];
                if (mx >= 0 && mx < gridSize && my >= 0 && my < gridColSize[0] && grid[mx][my]==0) {
                    grid[mx][my]=1;
                    queue[right][0]=mx;
                    queue[right++][1]=my;
                }
            }
        }
        step++;
    }
    return -1;
}